<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
/**
 * Create HTML widget with annotations and channels per movie/search
 * @author Matthijs van Henten <matthijs@waag.org>
 *
 *
 */
class Zend_View_Helper_AnnotationList{
	private $_annotations;

	/**
	 * This function is used as a pseudo constructor by Zend_View
	 * and may be called as a function.
	 *
	 * @param Default_Model_Movie $movie to show
	 * 		The __set method is used to parse the variables
	 *
	 * 	@return string $pager HTML code for the pager
	 */
	public function AnnotationList( array $annotations ){
		$this->_annotations = $annotations;
		return $this->getList();
	}

	/**
	 * Explicit render of the player
	 * @return REFACTOR! sloppy implementation, inline styles!
	 * @return string $HTML Html code for this pager
	 */
	public function getList(){
		$html = array();

		foreach( $this->_annotations as $annotation ){
			//AARG!
			ob_start();
			?>
		<div style="margin: 0 7px 7px 0; border-bottom: 1px dotted #999; width: 340px; overflow: hidden; float: left;">
		<h5>
			<?php echo $this->translate("Annotation #") . $annotation->id ?>
			<?php echo $this->translate('for movie') ?><br/>
			<a href="<?php echo $this->url(array('action' => 'play', 'id' => $annotation->movieId ), 'movie') ?>"><em><?php echo $annotation->getMovie()->title ?></em></a>
		</h5>
		<a href="<?php echo $this->url( array('action' => 'play', 'id' => $annotation->id ), 'annotation' ) ?>">
		<img style="display: inline; float: left; margin: 7px;" class="thumbnail" src="<?php echo $this->url( array('action' => 'thumbnail', 'id' => $annotation->movieId, 'timecode' => $annotation->getStart(false)), 'frame' ) ?>" />
			</a>
			<strong><span class="duration"><?php echo $this->translate("duration") ?> <?php echo $annotation->getDuration() ?></strong>
			<strong><?php echo $annotation->getStart() ?></strong>
				<dl style="display: inline;">
				<?php if( strlen($annotation->comment) > 0 ): ?>
					<dt><?php echo $this->translate("Comment:") ?></dt>
					<dd><?php echo $annotation->comment ?></dd>
				<?php else: ?>
					<dt><?php echo $this->translate("Keywords:") ?></dt>
					<dd><?php echo join( ", ", $annotation->getKeywords() ) ?></dd>
				<?php endif; ?>
				</dl>
		</div>
			<?php
			$html[] = ob_get_clean();
		}

		return join( "\n", $html );
	}

	/**
	 * render a single HTML string using a variable tagname, content and attributes
	 *
	 * @param string $tagName Something of a HTML tagName such as 'div'
	 * @param string $content Content to put into the tags
	 * @param array $attributes A key_value pair of attributes
	 *
	 * @return string $HTML
	 */
	public function getElement( $tagName, $content, $attributes=array() ){
		foreach( $attributes as $key => $value ){
			$attributes[$key] = sprintf('%s="%s"', $key, htmlspecialchars($value) );
		}

		return sprintf(
			"<%s%s>%s</%s>\n",
			$tagName,
			rtrim( ' ' . join(' ', $attributes) ),
			trim($content),
			$tagName
		);
	}

	/**
	 * placeholder. implement with Zend_Translation
	 * @return $string Output intput.
	 */
	private function translate( $string, $replace = array() ){
		return $string;
	}

	/**
	 * wrapper around getElement to make the function call shorter
	 *
	 * @param string $content Content to be wrapped in the link
	 * @param array $href urlOptions for url
	 * @param array $attributes Optional attributes
	 */
	public function getLink( $content, array $href, array $attributes = array() ){
		if( isset( $href['options'] ) ){
			// assume named params
			$template = array('options' => array(), 'name' => null, 'reset' => false, 'encode' => true );
			$href = array_merge( $template, $href );
			$attributes['href'] = call_user_func_array( array( $this, 'url' ), $href );
		}
		else{
			$attributes['href'] = $this->url( $href );
		}

		//$href = $this->url( $href );

		return $this->getElement( 'a', $content, $attributes );
	}

	/**
	 * Forward call to Zend_View_Helper_Url()
	 *
	 * @param array $urlOptions Options to set
	 * @param string $name Name of the named router
	 * @param bool $reset Reset current router (false)
	 * @param bool $encode Use urlencode
	 */
	public function url( array $urlOptions = array(), $name = null, $reset = false, $encode = true ){
		if( null == $url ){
			$url = new Zend_View_Helper_Url();
		}

		return $url->url( $urlOptions, $name, $reset, $encode );
	}
}
